﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalaryChannel.SCUserService;
namespace SalaryChannel
{
     public delegate void LoginCompleted();
    public partial class Login : UserControl
    {

        private LoginCompleted action = null;

        public LoginCompleted L_Action
        {
            set { action += value; }
            get
            {
                return action;
            }
        }
        public Login()
        {
            InitializeComponent();
            ServiceInstance.UserClient.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(dcWeb_LoginCompleted);
            this.PwdTB.KeyDown += new KeyEventHandler(PwdTB_KeyDown);
            ServiceInstance.UserClient.GetMyPwdCompleted += new EventHandler<GetMyPwdCompletedEventArgs>(UserClient_GetMyPwdCompleted);
            ServiceInstance.UserClient.GetPwdByEmailCompleted += new EventHandler<GetPwdByEmailCompletedEventArgs>(UserClient_GetPwdByEmailCompleted);
          
        }


        

        void PwdTB_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.Key == Key.Enter)
            {
                OKBtn_Click(null, null);
            }
        }

        private void OKBtn_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前请求正在处理中，请稍候");
                return;
            }
            if (string.IsNullOrEmpty(IDTB.Text)
                || string.IsNullOrEmpty(PwdTB.Password))
            {
                this.statusTB.Text = Properties.Resource.UserPwdWarning;
                return;
            }

            try
            {
                ServiceInstance.UserClient.LoginAsync(IDTB.Text, PwdTB.Password);
                loading1.Show();
            }
            catch 
            {

                this.statusTB.Text = "连接服务器失败，请稍后再试";
            }
           
        }

        void dcWeb_LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            // OKBtn.IsEnabled = true;
            string msg = string.Empty;
            try
            {
                if (e.Result ==null )
                {

                    msg = Properties.Resource.ConnectServerFailed;

                }
                else if (e.Result.UserID == "-1")
                {
                    msg = Properties.Resource.WrongIDorPwd;

                }
                else
                {
                   // App.Session.Add("CurrentUser", e.Result);               
                    App.CurrentUser = e.Result;

                    this.Visibility = Visibility.Collapsed;
                    this.action?.Invoke();

                }
            }
            catch 
            {
                msg = Properties.Resource.ConnectServerFailed;
            }
            finally
            {
                loading1.Stop();
                if (!string.IsNullOrEmpty(msg))
                {
                    App.MessageBox.ShowDialog(msg, Title.Information);
                }
            }

        }

        private void GetPwdBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前请求正在处理中，请稍候");
                return;
            }
            if (string.IsNullOrEmpty(IDTB.Text))
            {
                this.statusTB.Text = "请输入用户名";
                return;
            }           

            try
            {
                ServiceInstance.UserClient.GetPwdByEmailAsync(IDTB.Text);


                loading1.Show();
            }
            catch
            {

                this.statusTB.Text = "连接服务器失败，请稍后再试";
            }



            /*
            //内网版
        if (string.IsNullOrEmpty(IDTB.Text))
        {
            this.statusTB.Text = "请输入用户名,须和AM工号相同";
            return;
        }

        if (string.IsNullOrEmpty(this.PwdTB.Password))
        {
            this.statusTB.Text = "请输入AM密码";
            return;
        }

        try
        {
            ServiceInstance.UserClient.GetMyPwdAsync(IDTB.Text, PwdTB.Password);
              

            loading1.Show();
        }
        catch
        {

            this.statusTB.Text = "连接服务器失败，请稍后再试";
        }
         //*/
                //外网版
        }


        void UserClient_GetPwdByEmailCompleted(object sender, GetPwdByEmailCompletedEventArgs e)
        {
            try
            {
                if (e.Result == string.Empty)
                {
                    App.MessageBox.ShowDialog("操作成功，请注意查看邮件信息");
                }
                else
                {
                    App.MessageBox.ShowDialog("操作失败，请联系管理员修改密码：" + e.Result);
                }
            }
            catch (Exception ex)
            {

                App.MessageBox.ShowDialog("操作失败，" + ex.Message);
            }
            finally
            {
                loading1.Stop();
            }
        }


        void UserClient_GetMyPwdCompleted(object sender, GetMyPwdCompletedEventArgs e)
        {
            try
            {
                if (e.Result == "true")
                {
                    App.MessageBox.ShowDialog("操作成功，请注意查看邮件信息");
                }
                else
                {
                    App.MessageBox.ShowDialog("操作失败，" + e.Result);
                }
            }
            catch (Exception ex)
            {

                App.MessageBox.ShowDialog("操作失败，" + ex.Message);
            }
            finally
            {
                loading1.Stop();
            }
        }
        
    }
}
